/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.com.photop.sap.contactme.frame;

import cn.com.photop.sap.contactme.component.MyJTextField;
import cn.com.photop.sap.contactme.common.CommonConstant;
import cn.com.photop.sap.contactme.common.CommonSession;
import cn.com.photop.sap.contactme.model.Company;
import cn.com.photop.sap.contactme.model.ContactInfo;
import cn.com.photop.sap.contactme.model.Department;
import cn.com.photop.sap.contactme.service.CommonPropertiesService;
import cn.com.photop.sap.contactme.service.GetLocalContactInfoService;
import cn.com.photop.sap.contactme.service.GetMDBContactInfoService;
import cn.com.photop.sap.contactme.service.ImageService;
import cn.com.photop.sap.contactme.service.impl.ContactInfoService;
import cn.com.photop.sap.contactme.thread.*;
import cn.com.photop.sap.contactme.util.Configuration;
import cn.com.photop.sap.contactme.util.FileUtil;
import cn.com.photop.sap.contactme.util.StringComparator;
import cn.com.photop.sap.contactme.util.StringUtil;
import java.awt.Component;
import java.awt.Desktop;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultCellEditor;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 *
 * @author Jiancheng.Zheng
 */
public class MainJFrame extends javax.swing.JFrame {

    private Log logger = LogFactory.getLog(getClass());
    //所有的集合 用于树形的显示
    private Map< String, Map<String, List<ContactInfo>>> mapContactInfo;
    //所有人员的List
    private List<ContactInfo> listContactInfo;
    //公司的集合
    private List<Company> listCompany;
    //部门的集合 key 是公司
    private Map<String, List<Department>> mapDepartment;

    /**
     * Creates new form MainJFrame
     */
    public MainJFrame() throws FileNotFoundException, IOException, Exception {
//         logger.info( "projectPath="+CommonSession.getProjectPath() );
        mainJFrame = this;

        listContactInfo = new ArrayList<ContactInfo>();
        //设置通讯录对象内容
        GetMDBContactInfoService gmdbcis = new GetMDBContactInfoService();
        //所有的集合 用于树形的显示
        mapContactInfo = gmdbcis.getMapContactInfo();
        //所有人员的List
        listContactInfo = gmdbcis.getListContactInfo();
        //公司的集合
        listCompany = gmdbcis.getListCompany();

        //部门的集合 key 是公司
        mapDepartment = gmdbcis.getMapDepartment();

        initComponents();

        this.setLocationRelativeTo(null);
        //初始化JTable1的右键菜单
        initJTablePopupMenu();

        jMenu5.setVisible(false);
        jMenu6.setVisible(false);
        jMenu7.setVisible(false);

        jMenuItem2.setVisible(false);
        jMenuItem11.setVisible(false);
        jMenuItem12.setVisible(false);
        jMenuItem13.setVisible(false);
        //配置管理
        jMenuItem14.setVisible(false);

        //隐藏JTable ID字段
        jTable1.getTableHeader().getColumnModel().getColumn(0).setMaxWidth(0);
        jTable1.getTableHeader().getColumnModel().getColumn(0).setMinWidth(0);
        jTable1.getTableHeader().getColumnModel().getColumn(0).setPreferredWidth(0);
        jTable1.getTableHeader().getColumnModel().getColumn(0).setResizable(false);

//        jLabel5.setIcon(ImageService.getImageIcon("flower"));
//        jLabel4.setIcon(ImageService.getImageIcon("photop"));

        //去FTP服务器上下载最新数据
        if (CommonSession.getFtp_flg()) {
            //FTP下载新数据
            new DownDataThread().start();
            //FTP检索EXE软件更新，
            new UpgradFtpExeVerThread().start();
            //FTP检索SETUP软件更新，
            new UpgradFtpSetupVerThread().start();
        }

        //后台运行数据库ContactInfo数据更新
        new UpgradeDataThread().start();
        if (CommonSession.getLocal_flg()) {
            //REMOTE检索EXE软件更新，
            new UpgradeLocalExeVerThread().start();
            //REMOTE检索SETUP软件更新，
            new UpgradeLocalSetupVerThread( this ).start();
        }
        
        //后台更新EXCEL数据
        new UpgradeLocalExcelVerThread().start();

        //天气预报更新
        new WeatherThread( MainJFrame.this ).start();


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jMenuBar2 = new javax.swing.JMenuBar();
        jMenu3 = new javax.swing.JMenu();
        jMenu4 = new javax.swing.JMenu();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        jLabel1 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        //DefaultMutableTreeNode root = new DefaultMutableTreeNode(CommonConstant.CONSTANT_II_VI);
        //        Iterator it = mapContactInfo.entrySet().iterator();
        //        while (it.hasNext()) {
            //            Entry e = (Entry) it.next();
            //            String key = (String) e.getKey();
            //            DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(key);
            //            Map<String, List<ContactInfo>> m = (Map<String, List<ContactInfo>>) e.getValue();
            //            Iterator it1 = m.entrySet().iterator();
            //            while (it1.hasNext()) {
                //                Entry e1 = (Entry) it1.next();
                //                String key1 = (String) e1.getKey();
                //                DefaultMutableTreeNode child1_2 = new DefaultMutableTreeNode(key1);
                //                child1.add(child1_2);
                //                ArrayList<ContactInfo> ciList = (ArrayList<ContactInfo>) e1.getValue();
                //                for (int i = 0; i < ciList.size(); i++) {
                    //                    DefaultMutableTreeNode child1_2_3 = new DefaultMutableTreeNode(ciList.get(i).getName());
                    //                    child1_2.add(child1_2_3);
                    //                }
                //            }
            //
            //            root.add(child1);
            //
            //        }
        //        DefaultTreeModel model = new DefaultTreeModel(root);
        jTree1 = new javax.swing.JTree();
        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable() {

            public String getToolTipText(MouseEvent e) {
                int row = jTable1.rowAtPoint(e.getPoint());
                int col = jTable1.columnAtPoint(e.getPoint());
                String tiptextString = null;
                if (row > -1 && col > -1) {
                    Object value = jTable1.getValueAt(row, col);
                    if (null != value && !"".equals(value)) {
                        tiptextString = value.toString();//悬浮显示单元格内容
                    }
                }
                return tiptextString;
            }
            //表格不允许被编辑
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        jLabel5 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem14 = new javax.swing.JMenuItem();
        jMenuItem9 = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JPopupMenu.Separator();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu5 = new javax.swing.JMenu();
        jMenuItem8 = new javax.swing.JMenuItem();
        jMenuItem2 = new javax.swing.JMenuItem();
        jMenuItem11 = new javax.swing.JMenuItem();
        jMenu6 = new javax.swing.JMenu();
        jMenuItem10 = new javax.swing.JMenuItem();
        jMenuItem12 = new javax.swing.JMenuItem();
        jMenuItem13 = new javax.swing.JMenuItem();
        jMenu7 = new javax.swing.JMenu();
        jMenuItem7 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem3 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();
        jMenuItem1 = new javax.swing.JMenuItem();

        jMenu3.setText("File");
        jMenuBar2.add(jMenu3);

        jMenu4.setText("Edit");
        jMenuBar2.add(jMenu4);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("[未登陆]");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setResizable(false);

        jLabel1.setText("姓名");

        initJTree1();
        jTree1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jTree1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTree1MouseClicked(evt);
            }
        });
        jTree1.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                jTree1ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jTree1);

        jLabel2.setText("公司");

        //        List<String> companyStrList = new ArrayList<String>();
        //        companyStrList.add(0, CommonConstant.CONSTANT_II_VI);
        //        for (int i = 0; i < listCompany.size(); i++) {
            //            companyStrList.add(listCompany.get(i).getCompany());
            //        }
        //        final int size = companyStrList.size() + 1;
        //        String[] jModel = (String[]) companyStrList.toArray(new String[size]);
        //        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(jModel));
        //初始化JComboBox1的数据
        initJComboBox1();
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        jComboBox1.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jComboBox1PropertyChange(evt);
            }
        });
        jComboBox1.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
            public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                jComboBox1VetoableChange(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("宋体", 1, 30)); // NOI18N
        jLabel4.setText(CommonSession.getGroupName()+"通讯录");

        //jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
        //        jComboBox2.setSelectedIndex(0);
        initJComboBox2();

        jLabel3.setText("部门");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jButton1.setText("搜索");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        //jTable1.setEnabled(false);
        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null, null, null, null}
            },
            new String [] {
                "ID", "分公司", "部门", "姓名", "直线", "内线", "传真", "手机", "邮箱", "备注"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jTable1MousePressed(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);
        jTable1.getColumnModel().getColumn(1).setResizable(false);

        jLabel5.setText("姓名");

        jLabel6.setText("   ");
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel6MouseClicked(evt);
            }
        });

        jLabel7.setText("      ");
        jLabel7.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel7MouseClicked(evt);
            }
        });

        jLabel8.setText("      ");
        jLabel8.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel8MouseClicked(evt);
            }
        });

        jLabel9.setText("    ");
        jLabel9.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel9MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jLabel8)
                    .addComponent(jLabel7)
                    .addComponent(jLabel6))
                .addContainerGap(29, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel6)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel7)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel8)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel9))
        );

        jLabel6.setHorizontalAlignment( JLabel.CENTER );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField1)
                            .addComponent(jComboBox1, 0, 140, Short.MAX_VALUE))
                        .addGap(0, 132, Short.MAX_VALUE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jButton1))
                        .addGap(23, 23, 23))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(225, 225, 225)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel4)
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1)
                            .addComponent(jLabel5)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jMenu1.setText("文件");
        jMenu1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu1ActionPerformed(evt);
            }
        });

        jMenuItem4.setText("登陆管理");
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem4);

        jMenuItem14.setText("配置管理");
        jMenuItem14.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem14ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem14);

        jMenuItem9.setText("数据刷新");
        jMenuItem9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem9ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem9);
        jMenu1.add(jSeparator1);

        jMenuItem6.setText("退出软件");
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem6);

        jMenuBar1.add(jMenu1);

        jMenu5.setText("公司管理");

        jMenuItem8.setText("公司新增");
        jMenuItem8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem8ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem8);

        jMenuItem2.setText("公司修改");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem2);

        jMenuItem11.setText("公司删除");
        jMenuItem11.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem11ActionPerformed(evt);
            }
        });
        jMenu5.add(jMenuItem11);

        jMenuBar1.add(jMenu5);

        jMenu6.setText("部门管理");

        jMenuItem10.setText("部门新增");
        jMenuItem10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem10ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem10);

        jMenuItem12.setText("部门修改");
        jMenuItem12.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem12ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem12);

        jMenuItem13.setText("部门删除");
        jMenuItem13.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem13ActionPerformed(evt);
            }
        });
        jMenu6.add(jMenuItem13);

        jMenuBar1.add(jMenu6);

        jMenu7.setText("人员管理");

        jMenuItem7.setText("人员新增");
        jMenuItem7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem7ActionPerformed(evt);
            }
        });
        jMenu7.add(jMenuItem7);

        jMenuBar1.add(jMenu7);

        jMenu2.setText("帮助");

        jMenuItem3.setText("软件更新");
        jMenu2.add(jMenuItem3);

        jMenuItem5.setText("查看Excel");
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem5);

        jMenuItem1.setText("关于");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(33, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        AboutDialog aboutOkCancelDialog = new AboutDialog(null, true);
        aboutOkCancelDialog.show();
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField1ActionPerformed

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        //sout
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        // TODO add your handling code here:
//        System.out.println( jComboBox1.getSelectedIndex() );
        String selectedItem = (String) jComboBox1.getSelectedItem();
        if (CommonSession.getGroupName().equals(selectedItem)) {
            jComboBox2.removeAllItems();
            jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
            jComboBox2.setSelectedIndex(0);
            return;
        }
        //部门的集合 key 是公司
        List<Department> listDepartment = mapDepartment.get(selectedItem);
//        listDepartment.add(0, "All");
        List<String> departmentStrList = new ArrayList<String>();
        for (int i = 0; i < listDepartment.size(); i++) {
            departmentStrList.add(listDepartment.get(i).getDepartment());
        }
        int size = departmentStrList.size();
        if (size > 0) {
            String[] jModel = (String[]) departmentStrList.toArray(new String[size]);
            jComboBox2.removeAllItems();
            jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(jModel));
            jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
            jComboBox2.setSelectedIndex(0);
        } else {
            jComboBox2.removeAllItems();
            jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
            jComboBox2.setSelectedIndex(0);
        }


    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jComboBox1PropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jComboBox1PropertyChange
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1PropertyChange

    private void jComboBox1VetoableChange(java.beans.PropertyChangeEvent evt) throws java.beans.PropertyVetoException {//GEN-FIRST:event_jComboBox1VetoableChange
        // TODO add your handling code here:
    }//GEN-LAST:event_jComboBox1VetoableChange

    private void jTree1ValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_jTree1ValueChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_jTree1ValueChanged

    /**
     * 搜索按钮触发的处理
     *
     * @param evt
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        String company = jComboBox1.getSelectedItem().toString();
        String department = jComboBox2.getSelectedItem().toString();
        String name = jTextField1.getText();
//        System.out.println("Company:" + company + "|Department:" + department + "|Name:" + name);
        List<ContactInfo> ciList = new ArrayList<ContactInfo>();
        if (CommonSession.getGroupName().equals(company)) {
            //如果是集团 判断name 
            for (ContactInfo ci : listContactInfo) {
                //判断人名称
                if (StringUtil.isNotNull(name)) {
                    if (ci.getName().contains(name)) {
                        ciList.add(ci);
                    }
                } else {
                    ciList.add(ci);
                }
            }
        } else {
            Map<String, List<ContactInfo>> map1 = mapContactInfo.get(company);
            if (CommonConstant.CONSTANT_DEFAULT_DEPARTMENT.equals(department)) {
                //说明是All
                //遍历该公司的所有
                Iterator it = map1.entrySet().iterator();
                while (it.hasNext()) {
                    Entry e = (Entry) it.next();
                    List<ContactInfo> list1 = (List<ContactInfo>) e.getValue();
                    //判断人名称
                    for (ContactInfo ci : list1) {
                        //判断人名称
                        if (StringUtil.isNotNull(name)) {
                            if (ci.getName().contains(name)) {
                                ciList.add(ci);
                            }
                        } else {
                            ciList.add(ci);
                        }
                    }


                }

            } else {
                List<ContactInfo> list1 = map1.get(department);
                for (ContactInfo ci : list1) {
                    //判断人名称
                    if (StringUtil.isNotNull(name)) {
                        if (ci.getName().contains(name)) {
                            ciList.add(ci);
                        }
                    } else {
                        ciList.add(ci);
                    }
                }


            }
        }

        //设置JTable的值
        setJTableData(ciList);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * JTable 单击按钮
     *
     * @param evt
     */
    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        if (evt.getClickCount() == 2) {
//            jTable1.getSelectedRow();    
            int row = jTable1.rowAtPoint(evt.getPoint());
            int col = jTable1.columnAtPoint(evt.getPoint());
            int colNum = jTable1.getColumnCount();
            if (row > -1) {
                ContactInfo ci = getSelectedContactInfo(row);
                String company = ci.getCompany();
                //弹出框
                if (StringUtil.isNotNull(company)) {
                    DataDetailFrame ddf = new DataDetailFrame(ci);
                    ddf.setVisible(true);
                }

            }
//            for (int i = 0; i < colNum; i++) {
//                //循环获取值
////               jTable1.getValueAt( row, i );
//            }
//            
//            System.out.println("row:" + row + "|col:" + col);
//            if (row > -1 && col > -1) {
//                Object value = jTable1.getValueAt(row, col);
//                if (null != value && !"".equals(value)) {
//                }
//            }
        }
    }//GEN-LAST:event_jTable1MouseClicked
    private void jTree1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTree1MouseClicked
        //返回最后选择的节点
        DefaultMutableTreeNode selectedNode = (DefaultMutableTreeNode) jTree1.getLastSelectedPathComponent();
        //selectedNode.toString() 节点名称
        //selectedNode.getDepth() 节点深度
        //selectedNode.isLeaf() 是否是叶子
        //selectedNode.getParent().toString() 节点父类名称
        //集团名称：深度为3，否叶子
        //公司名称：深度为2，否叶子
        //部门名称：深度为1，否叶子
        //员工名称：深度为0，是叶子

        if (evt.getClickCount() == 2) {
            String department = new String();
            String company = new String();
            String name = new String();
            //获取选中的用户
            List<ContactInfo> listciTemp = new ArrayList<ContactInfo>();
//            System.out.println(selectedNode.getLevel());
            if (selectedNode.getLevel() == 2) {
                //表示是部门
                department = selectedNode.toString().trim();
                company = selectedNode.getParent().toString().trim();
                for (int i = 0; i < listContactInfo.size(); i++) {
                    ContactInfo ci = listContactInfo.get(i);
                    if (company.endsWith(ci.getCompany())
                            && department.endsWith(ci.getDepartment())) {
                        listciTemp.add(ci);

                    }
                }
            } else if (selectedNode.getLevel() == 1) {
                //表示是公司
                company = selectedNode.toString().trim();
//                System.out.println("Company:"+company);
                for (int i = 0; i < listContactInfo.size(); i++) {
                    ContactInfo ci = listContactInfo.get(i);
                    String companyTmp = ci.getCompany();
//                    System.out.println("companyTmp="+companyTmp );
                    if (company.endsWith(companyTmp)) {
                        listciTemp.add(ci);

                    }
                }

            } else if (selectedNode.getLevel() == 0) {
                //标示集团用户
                listciTemp = listContactInfo;
            } else if (selectedNode.getLevel() == 3) {
                //如果是也只的话要获取相对应的部门，公司
                name = selectedNode.toString().trim();
                department = selectedNode.getParent().toString().trim();
                company = selectedNode.getParent().getParent().toString().trim();
                for (int i = 0; i < listContactInfo.size(); i++) {
                    ContactInfo ci = listContactInfo.get(i);
                    if (company.endsWith(ci.getCompany())
                            && department.endsWith(ci.getDepartment())
                            && name.endsWith(ci.getName())) {
                        listciTemp.add(ci);

                    }
                }

            }
            //把获取到的listciTemp 设置到jTable1中去
            setJTableData(listciTemp);

        }
    }//GEN-LAST:event_jTree1MouseClicked

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        //打开Excel数据
        //获取配置文件
//        InputStream is = GetLocalContactInfoService.class.getResourceAsStream("/cn/com/photop/sap/contactme/config/common.properties");
//        Configuration rc = new Configuration(is);// 相对路径
//        String excelPath = rc.getValue("ExcelPath");

        String fix = CommonPropertiesService.getValue("Excel_Fix");
        String ext = CommonPropertiesService.getValue("updata_data_excel_ext");//".exe";
        String excelPath = CommonSession.getProjectPath() + CommonPropertiesService.getValue("ExcelPath");
        String excelFileName = "";
//        List<String> fileNames = Arrays.asList(FileUtil.getFileName(excelPath));
        List<String> fileNames = new ArrayList<String>();
        for ( String str:FileUtil.getFileName(excelPath) ){
            if ( str.toLowerCase().contains(fix.toLowerCase()) && str.endsWith(ext)) {
                fileNames.add( str );
            }
        }
        
        if (fileNames != null && !fileNames.isEmpty()) {
//            //剔除无关的文件数据
//            for (int i = 0; i < fileNames.size(); i++) {
//                if (!fileNames.get(i).toLowerCase().contains(fix.toLowerCase())
//                        || !fileNames.get(i).endsWith(ext)) {
//                    fileNames.remove(i);
//                    
//                }
//            }
            //判断版本最大的一个EXCEL文件
            //对集合对象进行排序         
            StringComparator comparator = new StringComparator();
            Collections.sort(fileNames, comparator);
            excelFileName = fileNames.get(0);
        }
        
        excelPath = excelPath + excelFileName;

        logger.info(excelPath);

        //获取本地文件的路径
        File file = new File(excelPath);
        try {
            Desktop.getDesktop().open(file);

        } catch (IOException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenu1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu1ActionPerformed
    }//GEN-LAST:event_jMenu1ActionPerformed

    /**
     * 点击管理MENU按钮
     *
     * @param evt
     */
    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        if (CommonSession.user == null) {
            LogonJFrame lf = new LogonJFrame(this);
            lf.setVisible(true);
        } else {
            String message = "确定要退出管理";
            int res = javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,
                    message, "提示", javax.swing.JOptionPane.OK_CANCEL_OPTION);
//            System.out.println("res:" + res);
            if (res == 0) {
                //确定
                CommonSession.user = null;
                setJMenuItem4Text("登陆管理");
                this.setTitle("[未登陆]");
//                jMenuItem7.setVisible(false);
//                jMenuItem8.setVisible(false);
//                jMenuItem10.setVisible(false);
                jMenu5.setVisible(false);
                jMenu6.setVisible(false);
                jMenu7.setVisible(false);
                //配置管理
                jMenuItem14.setVisible(false);

            }

        }

    }//GEN-LAST:event_jMenuItem4ActionPerformed

    /**
     * 退出软件
     *
     * @param evt
     */
    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        this.dispose();
    }//GEN-LAST:event_jMenuItem6ActionPerformed

    /**
     * JTABLE1 右键菜单的制作
     *
     * @param evt
     */
    private void jTable1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MousePressed
//        System.out.println("isPopupTrigger：" + evt.isPopupTrigger());
//
//        if (evt.isAltDown() || evt.isPopupTrigger()) {
        // 取得右键点击所在行
//            int row = evt.getY() / jTable1.getRowHeight();
//            // 弹出菜单
//            jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
//            /**
//             * 取得是表名的那一列
//             */
//            int tableNameColumn = -1;
//
//            for (int i = 0; i < jTable1.getColumnModel().getColumnCount(); i++) {
//                TableColumn selColumn = jTable1.getColumnModel().getColumn(i);
//                String columnHeader = (String) selColumn.getHeaderValue();
//
//                if (columnHeader.equals("分公司")) {
//                    tableNameColumn = i;
//                    break;
//                }
//            }
//            /**
//             * 取得表名并弹出菜单
//             */
//            if (tableNameColumn != -1) {
//                /**
//                 * 修改菜单首条的名称
//                 */
//                String tableName = (String) jTable1.getValueAt(row, tableNameColumn);
//                tableNameItem.setText(tableName);
//                // 弹出菜单
//                popupMenu.show(evt.getComponent(), evt.getX(), evt.getY());
//            }
//        }
    }//GEN-LAST:event_jTable1MousePressed

    private void jMenuItem10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem10ActionPerformed
        departmentAddFrame = new DepartmentAddFrame(this);
        departmentAddFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem10ActionPerformed

    private void jMenuItem7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem7ActionPerformed
        dataAddFrame = new DataAddFrame(this);
        dataAddFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem7ActionPerformed

    private void jMenuItem8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem8ActionPerformed
        companyAddJFrame = new CompanyAddJFrame(this);
        companyAddJFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem8ActionPerformed

    /**
     * 数据刷新
     *
     * @param evt
     */
    private void jMenuItem9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem9ActionPerformed

        freshData();

    }//GEN-LAST:event_jMenuItem9ActionPerformed

    private void jLabel6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseClicked
        if (evt.getClickCount() == 2) {
            if (weatherCityChangeFrame == null || !weatherCityChangeFrame.isVisible()) {
                weatherCityChangeFrame = new WeatherCityChangeFrame(this);
                weatherCityChangeFrame.setVisible(true);
            }
        }

    }//GEN-LAST:event_jLabel6MouseClicked

    private void jLabel7MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel7MouseClicked
        if (evt.getClickCount() == 2) {
            if (weatherCityChangeFrame == null || !weatherCityChangeFrame.isVisible()) {
                weatherCityChangeFrame = new WeatherCityChangeFrame(this);
                weatherCityChangeFrame.setVisible(true);
            }
        }
    }//GEN-LAST:event_jLabel7MouseClicked

    private void jLabel8MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel8MouseClicked
        if (evt.getClickCount() == 2) {
            if (weatherCityChangeFrame == null || !weatherCityChangeFrame.isVisible()) {
                weatherCityChangeFrame = new WeatherCityChangeFrame(this);
                weatherCityChangeFrame.setVisible(true);
            }
        }
    }//GEN-LAST:event_jLabel8MouseClicked

    /**
     * 部门修改
     *
     * @param evt
     */
    private void jMenuItem12ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem12ActionPerformed
        DepartmentEditFrame departmentEditFrame = new DepartmentEditFrame(this);
        departmentEditFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem12ActionPerformed

    /**
     * 配置参数修改
     *
     * @param evt
     */
    private void jMenuItem14ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem14ActionPerformed
        ConfigEditFrame configEditFrame = new ConfigEditFrame();
        configEditFrame.setVisible(true);

    }//GEN-LAST:event_jMenuItem14ActionPerformed

    private void jMenuItem13ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem13ActionPerformed
        DepartmentDeleteFrame departmentDeleteFrame = new DepartmentDeleteFrame(this);
        departmentDeleteFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem13ActionPerformed

    /**
     * 修改公司名称
     *
     * @param evt
     */
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        CompanyEditFrame companyEditFrame = new CompanyEditFrame(this);
        companyEditFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jLabel9MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel9MouseClicked
        if (evt.getClickCount() == 2) {
            if (weatherCityChangeFrame == null || !weatherCityChangeFrame.isVisible()) {
                weatherCityChangeFrame = new WeatherCityChangeFrame(this);
                weatherCityChangeFrame.setVisible(true);
            }
        }
    }//GEN-LAST:event_jLabel9MouseClicked

    /**
     * 删除公司
     *
     * @param evt
     */
    private void jMenuItem11ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem11ActionPerformed
        CompanyDeleteFrame companyDeleteFrame = new CompanyDeleteFrame(this);
        companyDeleteFrame.setVisible(true);
    }//GEN-LAST:event_jMenuItem11ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;


                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new MainJFrame().setVisible(true);


                } catch (FileNotFoundException ex) {
                    Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (IOException ex) {
                    Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
                } catch (Exception ex) {
                    Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenu jMenu5;
    private javax.swing.JMenu jMenu6;
    private javax.swing.JMenu jMenu7;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem10;
    private javax.swing.JMenuItem jMenuItem11;
    private javax.swing.JMenuItem jMenuItem12;
    private javax.swing.JMenuItem jMenuItem13;
    private javax.swing.JMenuItem jMenuItem14;
    private javax.swing.JMenuItem jMenuItem2;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JMenuItem jMenuItem7;
    private javax.swing.JMenuItem jMenuItem8;
    private javax.swing.JMenuItem jMenuItem9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPopupMenu.Separator jSeparator1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
    private MainJFrame mainJFrame;
    private CompanyAddJFrame companyAddJFrame;
    private DataAddFrame dataAddFrame;
    private DepartmentAddFrame departmentAddFrame;
    private WeatherCityChangeFrame weatherCityChangeFrame;

    /**
     * 设置JTable的值
     *
     * @param ciList
     */
    private void setJTableData(List<ContactInfo> ciList) {

        //把获取到的listciTemp 设置到jTable1中去
        ContactInfo ci = new ContactInfo();
        DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();
        int rowcount = tableModel.getRowCount();
        for (int i = 0; i < rowcount; i++) {
            //清空原来数据
            tableModel.removeRow(0);
        }

        int cilen = ciList.size();
        int i = 0;
        for (int j = 0; j < ciList.size(); j++) {
            ContactInfo citmp = ciList.get(j);
            String[] rowData = new String[]{
                citmp.getId(),
                citmp.getCompany(),
                citmp.getDepartment(),
                citmp.getName(),
                citmp.getTel(),
                citmp.getInside(),
                citmp.getFax(),
                citmp.getMobile(),
                citmp.getEmail(),
                citmp.getRemark()
            };
            tableModel.insertRow(i++, rowData);
        }
        //补齐空的JTable内容
        if (ciList.size() < 12) {
            for (int k = ciList.size(); k < 12; k++) {
                String[] rowData = new String[]{
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    "",
                    ""};
                tableModel.insertRow(k, rowData);
            }
        }

        jTable1.clearSelection();

    }

    /**
     * 设置 登陆管理 退出管理 MenuItem4的文本设置
     *
     * @param title
     */
    public void setJMenuItem4Text(String title) {
        jMenuItem4.setText(title);
    }

    /**
     * JTable1的右键菜单
     */
    private void initJTablePopupMenu() {
        //为Table增加右键菜单
        final JPopupMenu jPopupMenu1 = new JPopupMenu();

        JMenuItem detailItem = new JMenuItem("查看");
        JMenuItem editItem = new JMenuItem("编辑");
        JMenuItem deleteItem = new JMenuItem("删除");

//        jPopupMenu1.addSeparator();
        jPopupMenu1.add(detailItem);
        jPopupMenu1.add(editItem);
        jPopupMenu1.add(deleteItem);
//        jPopupMenu1.addSeparator();


        jTable1.addMouseListener(new MouseAdapter() {

            public void mousePressed(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }

            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    showMenu(e);
                }
            }

            private void showMenu(MouseEvent e) {
                int row = jTable1.getSelectedRow();
                String company = null;
                if (row != -1) {
                    company = (String) jTable1.getValueAt(row, 0);
                }

                if (jTable1.getSelectedRowCount() > 0 && StringUtil.isNotNull(company) && CommonSession.user != null) {
                    jPopupMenu1.show(e.getComponent(), e.getX(), e.getY());
                }

            }
        });

        //查看那右键
        detailItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                int row = jTable1.getSelectedRow();
                ContactInfo ci = getSelectedContactInfo(row);
                String company = ci.getCompany();

                //弹出框
                if (StringUtil.isNotNull(company)) {
                    DataDetailFrame ddf = new DataDetailFrame(ci);
                    ddf.setVisible(true);
                }
            }
        });

        //编辑
        editItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
                int row = jTable1.getSelectedRow();
                ContactInfo ci = getSelectedContactInfo(row);
                String company = ci.getCompany();
                //弹出框
                if (StringUtil.isNotNull(company)) {
                    DataEditFrame def = new DataEditFrame(ci, jTable1, listContactInfo);
                    def.setVisible(true);
                }
            }
        });

        //删除
        deleteItem.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent arg0) {
//                System.out.println("delete Item");
                String message = "确定要删除该条记录？";
                int res = javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,
                        message, "提示", javax.swing.JOptionPane.OK_CANCEL_OPTION);
//            System.out.println("res:" + res);
                if (res == 0) {
                    //确定
                    int row = jTable1.getSelectedRow();
                    //获取其用户信息 然后做删除处理
                    ContactInfo ci = getSelectedContactInfo(row);
                    //删除数据库中的用户
                    ContactInfoService contactInfoService = new ContactInfoService();
                    contactInfoService.delete(ci);
                    //删除系统中的数据
                    //新增系统中的数据
                    for (int i = 0; i < listContactInfo.size(); i++) {
                        if (listContactInfo.get(i).getId().equals(ci.getId())) {
                            listContactInfo.remove(i);
                        }
                    }
//                    listContactInfo.remove(ci);
                    //移除JTable1的数据
                    DefaultTableModel tableModel = (DefaultTableModel) jTable1.getModel();
                    tableModel.removeRow(row);
                    mapContactInfo.get(ci.getCompany()).get(ci.getDepartment()).remove(ci);
                    //初始化JTree1
                    initJTree1();


                }
            }
        });

        //Jtable单元格的默认文本编辑器   
//    DefaultCellEditor objectDefaultCellEditor = new DefaultCellEditor(new MyJTextField()); 
//    jTable1.setDefaultEditor(Object.class,objectDefaultCellEditor);//为Object类型指定默认编辑器  

    }

    /**
     *
     * @return
     */
    public JMenuItem getJMenuItem7() {
        return jMenuItem7;
    }

    /**
     *
     * @return
     */
    public JMenuItem getJMenuItem8() {
        return jMenuItem8;
    }

    /**
     *
     * @return
     */
    public JMenuItem getJMenuItem10() {
        return jMenuItem10;
    }

    public List<Company> getListCompany() {
        return listCompany;
    }

    public Map<String, List<Department>> getMapDepartment() {
        return mapDepartment;
    }

    public Map< String, Map<String, List<ContactInfo>>> getMapContactInfo() {
        return mapContactInfo;
    }

    public List<ContactInfo> getListContactInfo() {
        return listContactInfo;
    }

    public JLabel getJLabel6() {
        return jLabel6;
    }

    public JLabel getJLabel7() {
        return jLabel7;
    }

    public JLabel getJLabel8() {
        return jLabel8;
    }

    public JLabel getJLabel9() {
        return jLabel9;
    }

    /**
     * 获取JTable中选择中的ContactInfo数据
     */
    public ContactInfo getSelectedContactInfo(int row) {
        String id = (String) jTable1.getValueAt(row, 0);
        String company = (String) jTable1.getValueAt(row, 1);
        String department = (String) jTable1.getValueAt(row, 2);
        String name = (String) jTable1.getValueAt(row, 3);
        String tel = (String) jTable1.getValueAt(row, 4);
        String inside = (String) jTable1.getValueAt(row, 5);
        String fax = (String) jTable1.getValueAt(row, 6);
        String mobile = (String) jTable1.getValueAt(row, 7);
        String email = (String) jTable1.getValueAt(row, 8);
        String remark = (String) jTable1.getValueAt(row, 9);

        ContactInfo ci = new ContactInfo();
        if (StringUtil.isNotNull(id)) {
            ci.setId(id);
        }
        if (StringUtil.isNotNull(company)) {
            ci.setCompany(company);
        }
        if (StringUtil.isNotNull(department)) {
            ci.setDepartment(department);
        }
        if (StringUtil.isNotNull(name)) {
            ci.setName(name);
        }
        if (StringUtil.isNotNull(tel)) {
            ci.setTel(tel);
        }
        if (StringUtil.isNotNull(inside)) {
            ci.setInside(inside);
        }
        if (StringUtil.isNotNull(fax)) {
            ci.setFax(fax);
        }
        if (StringUtil.isNotNull(mobile)) {
            ci.setMobile(mobile);
        }
        if (StringUtil.isNotNull(email)) {
            ci.setEmail(email);
        }
        if (StringUtil.isNotNull(remark)) {
            ci.setRemark(remark);
        }
        return ci;
    }

    /**
     * 初始化JCOMBOBOX1的数据
     */
    public void initJComboBox1() {
        List<String> companyStrList = new ArrayList<String>();
        companyStrList.add(0, CommonSession.getGroupName());
        for (int i = 0; i < listCompany.size(); i++) {
            companyStrList.add(listCompany.get(i).getCompany());
        }
        final int size = companyStrList.size() + 1;
        String[] jModel = (String[]) companyStrList.toArray(new String[size]);
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(jModel));
    }

    /**
     * 初始化JComboBox2
     */
    public void initJComboBox2() {
        if (!CommonConstant.CONSTANT_DEFAULT_DEPARTMENT.equals((String) jComboBox2.getSelectedItem())) {
            jComboBox2.insertItemAt(CommonConstant.CONSTANT_DEFAULT_DEPARTMENT, 0);
            jComboBox2.setSelectedIndex(0);
        }
    }

    /**
     * 初始化JTree1
     */
    public void initJTree1() {
        DefaultMutableTreeNode root = new DefaultMutableTreeNode(CommonSession.getGroupName());
        Iterator it = mapContactInfo.entrySet().iterator();
        while (it.hasNext()) {
            Entry e = (Entry) it.next();
            String key = (String) e.getKey();
            DefaultMutableTreeNode child1 = new DefaultMutableTreeNode(key);
            Map<String, List<ContactInfo>> m = (Map<String, List<ContactInfo>>) e.getValue();
            Iterator it1 = m.entrySet().iterator();
            while (it1.hasNext()) {
                Entry e1 = (Entry) it1.next();
                String key1 = (String) e1.getKey();
                DefaultMutableTreeNode child1_2 = new DefaultMutableTreeNode(key1);
                child1.add(child1_2);
                ArrayList<ContactInfo> ciList = (ArrayList<ContactInfo>) e1.getValue();
                for (int i = 0; i < ciList.size(); i++) {
                    DefaultMutableTreeNode child1_2_3 = new DefaultMutableTreeNode(ciList.get(i).getName());
                    child1_2.add(child1_2_3);
                }
            }

            root.add(child1);

        }
        DefaultTreeModel model = new DefaultTreeModel(root);
        jTree1.setModel(model);
    }

    public javax.swing.JTable getJTable1() {
        return jTable1;
    }

    /**
     * 菜单：公司管理
     *
     * @return
     */
    public javax.swing.JMenu getJMenu5() {
        return jMenu5;
    }

    /**
     * 菜单：部门管理
     *
     * @return
     */
    public javax.swing.JMenu getJMenu6() {
        return jMenu6;
    }

    /**
     * 菜单：人员管理
     *
     * @return
     */
    public javax.swing.JMenu getJMenu7() {
        return jMenu7;
    }

    /**
     * 公司修改
     *
     * @return
     */
    public javax.swing.JMenuItem getjMenuItem2() {
        return jMenuItem2;
    }

    /**
     * 公司删除
     *
     * @return
     */
    public javax.swing.JMenuItem getjMenuItem11() {
        return jMenuItem11;
    }

    /**
     * 部门修改
     *
     * @return
     */
    public javax.swing.JMenuItem getjMenuItem12() {
        return jMenuItem12;
    }

    /**
     * 部门删除
     *
     * @return
     */
    public javax.swing.JMenuItem getjMenuItem13() {
        return jMenuItem13;
    }

    /**
     * 配置管理
     *
     * @return
     */
    public javax.swing.JMenuItem getjMenuItem14() {
        return jMenuItem14;
    }

    /**
     * 数据更新
     */
    public void freshData() {
        try {
            //设置通讯录对象内容
            GetMDBContactInfoService gmdbcis = new GetMDBContactInfoService();
            //所有的集合 用于树形的显示
            mapContactInfo = null;
            mapContactInfo = gmdbcis.getMapContactInfo();
            //所有人员的List
            listContactInfo = null;
            listContactInfo = gmdbcis.getListContactInfo();
            //公司的集合
            listCompany = null;
            listCompany = gmdbcis.getListCompany();
            //部门的集合 key 是公司
            mapDepartment = null;
            mapDepartment = gmdbcis.getMapDepartment();

//            Map<String, List<Department>>
//            Iterator it = mapDepartment.entrySet().iterator();
//            while (it.hasNext()) {
//                Entry entry = (Entry) it.next();
//                String key = (String) entry.getKey();
////                System.out.println( key );
//                List<Department> depList = (List<Department>) entry.getValue();
//                for (Department d : depList) {
//                    System.out.println(d.getDepartment());
//
//                }
//            }

            initJComboBox1();
            initJComboBox2();
            initJTree1();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(MainJFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.setVisible(true);
    }

    public void setMapContactInfo(Map< String, Map<String, List<ContactInfo>>> mapContactInfo) {
        this.mapContactInfo = mapContactInfo;
    }
    //所有人员的List

    public void setListContactInfo(List<ContactInfo> listContactInfo) {
        this.listContactInfo = listContactInfo;
    }
    //公司的集合

    public void setListCompany(List<Company> listCompany) {
        this.listCompany = listCompany;
    }
    //部门的集合 key 是公司

    public void setMapDepartment(Map<String, List<Department>> mapDepartment) {
        this.mapDepartment = mapDepartment;
    }
}
